/** 
 * User: seeker910
 * Date: 13-10-24
 * Time: 下午10:22

 */
/*
 * */
Rsd.define('Rsd.form.Input', {
    extend: 'Rsd.control.Component',
    xtype: 'input',
    height:30,
    width:300,
    //margin:'3 0 2 0',
    ctrlTagName: 'input',
    inputType: 'text',
    //ctrl name 属性
    dataIndex:'',
    //value: null,
    readonly: false, 
    disabled:false,
    required:false,
    placeholder:'',
    accessKey:'',
    /**
     * 同 onchange 事件 (兼容) ，不建议使用，建议使用onchange代替
     */
    valueChanged:null,
    /*
    * 同 onchange 事件 (兼容) ，不建议使用，建议使用onchange代替
    * */
    textChanged:null,
    /** 
    * onchange 事件 (兼容)
    * */
    onchange:null,
    /**
     * 
     */
    onblur:null,
    /*
    *
    * */
    constructor: function Input (config) {
        config = config || {};
        this.apply(config);
    },
     /*
     * */
    onAfterInit: function onAfterInit() {
        this.callParent();
        var me = this;
        me.dom.style.textAlign = 'left';
       
        var _position = this.label.position||'top';
        
        switch (_position.toLowerCase()) {
            case 'top':
                { 
                } 
                break;
            case 'bottom':
                { 
                } 
                break;
            case 'left':
                { 
                     //重新设置 input 高度
                    me.ctrl.element.style.height = "calc(100% - 3%)";
                }
                break;
            case 'right':
                { 
                     //重新设置 input 高度
                    me.ctrl.element.style.height = "calc(100% - 3%)";
                }
                break
            default:
                throw new Error('Property position of header or label is unknown value ['+this.label.position+'].');
                break;
        }  
       
        me.ctrl.element.type = me.inputType;
        me.ctrl.element.name = me.name || me.dataIndex ;
        if(me.readonly)
        {
            me.ctrl.element.setAttribute('readonly',true);
        }
        //me.ctrl.element.readonly = me.readonly;
        if(me.disabled)
        {
            me.ctrl.element.setAttribute('disabled',true);
        }
        //me.ctrl.element.disabled = me.disabled;
        me.ctrl.element.required = me.required?'required':'';
        me.ctrl.element.placeholder = (me.placeholder||'') + (me.required?'(必填)':'' ) ;
        me.ctrl.element.autocomplete="off";
        me.ctrl.element.accessKey = me.accessKey;
        
        //console.log('input ctrl',me.dataIndex,me,me.onchange||me.valueChanged||me.textChanged);
        //
        if(me.onchange||me.valueChanged||me.textChanged)
        {
            me.ctrl.element.onchange = function (evt) { 
                me.funApplyByIOC(me.onchange||me.valueChanged||me.textChanged,[me,evt]);
            }
        } 
        if(me.onblur)
        {
            me.ctrl.element.onblur = function(evt)
            {
                me.funApplyByIOC(me.onblur,[me,evt]);
            }
        }
        
        if (me.__value != undefined) {
            //firefox 不支持 innerText
            me.ctrl.element.innerHTML = me.__value;
            me.ctrl.element.value = me.__value;
        }

    },
    /**
     * 
     * @param {*} disabled 
     */
    setDisabled: function setDisabled(disabled) {
        this.callParent(disabled);
        if( this.ctrl.element)
        {
            this.ctrl.element.disabled = this.disabled;
        } 
    },
    /*
    * */
    setValue: function setValue(value) {
        var me = this;
        if(me.inputType == 'number')
        { 
            if(Rsd.isNumber(value))
            {
                me.__value = value;
            }
            else
            {
                if(Rsd.isString(value))
                {
                    me.__value = Number(value);
                }
                else
                { 
                    me.__value = 0;
                }
            } 
        }
        else
        {
            me.__value = value;
        }
     
        if (me.ctrl.element) { 
            me.ctrl.element.value = me.__value;

            //模拟触发onchange
            //me.ctrl.element.focus();
            //me.ctrl.element.blur();

            if ("createEvent" in document) {
                var evt = document.createEvent("HTMLEvents");
                evt.initEvent("change", false, true);
                me.ctrl.element.dispatchEvent(evt);
            }
            else
            {
                me.ctrl.element.fireEvent("onchange");
            }
        }
    },

    /*
    * */
    getValue: function getValue() {
        var me = this;
        if(me.inputType == 'hidden')
        {
            return me.__value;
        }
        return  me.ctrl.element ? me.ctrl.element.value : me.__value;
    },
    /*
    * defaultValue 是控件初次加载时设置的值，可用于判断控件值是否改变。
    * */
    getDefaultValue: function getDefaultValue() {
        var me = this;
        return me.ctrl.element ? me.ctrl.element.defaultValue : me.value
    },
    /*
    *
    * */
    checkValue:function checkValue() {
        //debugger;
        this.error='';
        this.ctrl.element.title=this.tip||'';
        this.removeCls('ctrl','x-control-error');
        if(this.required)
        {
            if(this.ctrl.element.value == '' )
            {
                this.error = '【' + this.label.content + '】必填';
                this.ctrl.element.title = this.error;
                this.addCls('ctrl','x-control-error');
                return false;
            }

        }
        return true;
    }

},function (type) {

    var _valueGetter = function () {
        return this.getValue();
    };

    var _valueSetter = function (value) {
        this.setValue(value);
    };
    this.defineProperty(type,"value", _valueGetter, _valueSetter,true);
});
